package main

import (
	"fmt"
	"gitee.com/hlwqds/rabitmqTest/config"
	"gitee.com/hlwqds/rabitmqTest/log"
	"gitee.com/hlwqds/rabitmqTest/rabbitmqInAction/forgetAfterPushModel/forgetConfig"
	"github.com/streadway/amqp"
)

func main(){
	conn, err := amqp.Dial(config.GenURLFromConfig())
	log.FailOnError(err, "amqp.Dial error")
	ch, err := conn.Channel()
	log.FailOnError(err, "conn.Channel error")

	//声明topic类型来路由不同等级的日志消息
	ch.ExchangeDeclare(
		forgetConfig.ForgetExchange,
		"topic",
		false,
		false,
		false,
		false,
		nil,
		)

	//声明队列并绑定exchange
	ch.QueueDeclare(
		forgetConfig.ForgetQueueCritical,
		false,
		false,
		false,
		false,
		nil,
		)
	ch.QueueBind(
		forgetConfig.ForgetQueueCritical,
		forgetConfig.ForgetQueueCriticalBindKey,
		forgetConfig.ForgetExchange,
		false,
		nil,
		)

	ch.QueueDeclare(
		forgetConfig.ForgetQueueRateLimit,
		false,
		false,
		false,
		false,
		nil,
	)
	ch.QueueBind(
		forgetConfig.ForgetQueueRateLimit,
		forgetConfig.ForgetQueueRateLimitBindKey,
		forgetConfig.ForgetExchange,
		false,
		nil,
	)

	criticalCh, err := ch.Consume(
		forgetConfig.ForgetQueueCritical,
		"critical",
		false,
		false,
		false,
		false,
		nil,
		)
	rateLimitCh, err := ch.Consume(
		forgetConfig.ForgetQueueRateLimit,
		"rateLimit",
		false,
		false,
		false,
		false,
		nil,
		)

	for {
		var data amqp.Delivery
		select{
		case data = <-criticalCh:
		case data = <-rateLimitCh:
		}
		data.Ack(false)
		fmt.Println(data.Body)
	}
}